package com.hundsun.s3.mrs.core.chain;

import java.util.ArrayList;
import java.util.List;

import com.hundsun.s3.mrs.core.IParamsIn;
import com.hundsun.s3.mrs.core.IParamsOut;
import com.hundsun.s3.mrs.core.handler.IHandler;

/**
 * @author chenji23607
 *
 */
public class Chain implements IChain {

	/**
	 * 任务链
	 */
	private List<IHandler> handlers = new ArrayList<>();
	
	private int index = 0;
	
	private String desc;
	
	private String chainName;
	
	
	public String getDesc() {
		return desc;
	}

	public void setDesc(String desc) {
		this.desc = desc;
	}

	public void setChainName(String chainName) {
		this.chainName = chainName;
	}

	@Override
	public void doHandle(IParamsIn in, IParamsOut out, IChain chain) {
		if(index == handlers.size()) {
			// 如果已经被任务链中的某一环所处理或执行到最后一个任务处理器，执行结束
			index = 0;
			return;
		}
		IHandler h = handlers.get(index);
		index++;
		h.doHandle(in, out, chain);
	}

	public int getIndex() {
		return index;
	}

	public void setIndex(int index) {
		this.index = index;
	}

	public void setHandlers(List<IHandler> handlers) {
		this.handlers = handlers;
	}

	@Override
	public List<IHandler> getHandlers() {
		return this.handlers;
	}

	@Override
	public void removeHandler(IHandler handler) {
		this.handlers.remove(handler);
	}

	@Override
	public void addHandler(IHandler handler) {
		this.handlers.add(handler);
	}

	public String getChainName() {
		return chainName;
	}
	
}
